package com.fmsz.xmly.fragments;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.fmsz.xmly.R;
import com.fmsz.xmly.adapters.FindVPAdapter;
import com.fmsz.xmly.asynctasks.FindTabDown;
import com.fmsz.xmly.callbacks.InterfaceClass;
import com.fmsz.xmly.entitys.FindTabEntity;
import com.fmsz.xmly.fragments.findfgts.FindAnchorFragment;
import com.fmsz.xmly.fragments.findfgts.FindBroadcastFragment;
import com.fmsz.xmly.fragments.findfgts.FindClassifyFragment;
import com.fmsz.xmly.fragments.findfgts.FindRecommendFragment;
import com.fmsz.xmly.fragments.findfgts.FindTopListFragment;
import com.fmsz.xmly.utils.Urls;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class FindFragment extends Fragment implements ViewPager.OnPageChangeListener,InterfaceClass.FindTabCallback{
    private Fragment[] findFgtArr;
    private List<FindTabEntity> tabList=new ArrayList<>();
    private ImageView find_search_iv;
    private TabLayout find_tab;
    private ViewPager find_vp;
    private FindVPAdapter adapter;
    public FindFragment() {
        // Required empty public constructor
        //测试
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_find, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        find_search_iv=(ImageView)view.findViewById(R.id.find_search_iv);
        find_tab=(TabLayout)view.findViewById(R.id.find_tab);
        find_vp=(ViewPager)view.findViewById(R.id.find_vp);
        find_vp.addOnPageChangeListener(this);

        new FindTabDown(this).execute(Urls.FIND_TAB);
        initFgt();

    }
    //实例化
    private void initFgt() {
        findFgtArr=new Fragment[]{new FindRecommendFragment(),new FindClassifyFragment(),new FindBroadcastFragment(),new FindTopListFragment(),new FindAnchorFragment()};

    }
//    ViewPager滑动监听
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
//标题栏接口回调
    @Override
    public void findTabCallback(List<FindTabEntity> list) {
        tabList.addAll(list);
        adapter=new FindVPAdapter(getChildFragmentManager(),tabList,findFgtArr);
        find_vp.setAdapter(adapter);
        find_tab.setupWithViewPager(find_vp);
        find_tab.getTabAt(0).select();
    }
}
